//
//  SourceDetailViewController.m
//  zhijian
//
//  Created by lcb on 2019/4/6.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "SourceDetailViewController.h"
#import "ListChartView.h"
#import "SourceDetailModel.h"
#import "TabBarHeaderView.h"
#import <TYPagerController/TYTabPagerBar.h>
@interface TabbModel : BaseModel
@property(nonatomic,strong) NSString* sid;
@property(nonatomic,strong) NSString* title;
@end
@implementation TabbModel
@end

@interface SourceDetailViewController ()<ListChartViewDelegate,TYTabPagerBarDelegate,TYTabPagerBarDataSource>
{
    
}
@property(nonatomic,strong) ListChartView *chatView;
@property(nonatomic,strong) SourceDetailModel*sourceDetailModel;
@property(nonatomic,strong) NSMutableArray *sidsArray;
@property(nonatomic,strong) NSMutableArray *tabsArray;
@property(nonatomic,strong) UILabel *remarkLab;
@property(nonatomic,strong) TYTabPagerBar *tabBar;
@property(nonatomic,assign) NSInteger currentIndex;
@end

@implementation SourceDetailViewController
@synthesize sourceModel=_SourceModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self hyb_navWithLeftImage:@"back" title:@"对比详情" leftClicked:^(UIButton *sender) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    if (!_sidsArray) {
        _sidsArray =[NSMutableArray array];
    }
    [self.sidsArray removeAllObjects];
    if (!_tabsArray) {
        _tabsArray=[NSMutableArray array];
    }
    _currentIndex =0;
    [self.tabsArray removeAllObjects];
    NSString *sid =self.sourceModel.sameSourceIds;
    NSArray *sids = [sid componentsSeparatedByString:@","];
    for (int i = 0; i<sids.count; i++) {
        NSString *sstile = [sids objectAtIndex:i];
        TabbModel *tab =[TabbModel new];
        tab.sid = sstile;
        tab.title = [NSString stringWithFormat:@"标签%d",i+1];
        [self.sidsArray addObject:tab];
        [self.tabsArray addObject:tab.title];
    }
    _chatView = [[ListChartView alloc]initWithFrame:CGRectMake(10, 100, SCREEN_WIDTH-20, SCREEN_HEIGHT-100)];
    [_chatView setBorderWidth:0.5];
    [_chatView setDelegate:self];
    [self.view addSubview:_chatView];
    if (self.sidsArray.count>1) {
        self.tabBar.frame=CGRectMake(0, SCREEN_HEIGHT-40, SCREEN_WIDTH, 40);
        self.remarkLab.frame =CGRectMake(10, SCREEN_HEIGHT-90, SCREEN_WIDTH-20, 40);
        [self.tabBar reloadData];
    }
    TabbModel *tab =self.sidsArray.firstObject;
    [self loadData:tab.sid];
}
-(NSInteger)rowForList:(ListChartView *)list
{
    if (self.sourceDetailModel.kinds>=6){
        return 8;
    }else{
         return 6;
    }
}
-(NSInteger)columnForList:(ListChartView *)list
{
    return 3;
}

-(CGSize)listChart:(ListChartView *)list itemSizeForRow:(NSInteger)row column:(NSInteger)column
{
    return CGSizeMake((SCREEN_WIDTH-20)/3, 60.0f);
}

-(NSString*)listChart:(ListChartView *)list textForRow:(NSInteger)row column:(NSInteger)column
{
    if (row == 0)
    {
        if (self.sourceDetailModel.kinds>=6) {
            IF_T_FF(row, column, 0, 0){return @"";}
            EF_T_FF(row, column, 0, 1){return @"韩国";}
            EF_T_FF(row, column, 0, 2){return @"中国";}
        }else{
            IF_T_FF(row, column, 0, 0){return @"";}
            EF_T_FF(row, column, 0, 1){return @"欧盟";}
            EF_T_FF(row, column, 0, 2){return @"中国";}
        }
        
    }
    else if (column == 0)
    {
        if (self.sourceDetailModel.kinds>=6){
            IF_T_FF(row, column, 1, 0){return @"序号";}
            EF_T_FF(row, column, 2, 0){return @"名称";}
            EF_T_FF(row, column, 3, 0){return @"CI号";}
            EF_T_FF(row, column, 4, 0){return @"各种化妆品";}
            EF_T_FF(row, column, 5, 0){return @"除眼部化妆品之外的其他化妆品";}
            EF_T_FF(row, column, 6, 0){return @"专用于不与粘膜接触的化妆品";}
            EF_T_FF(row, column, 7, 0){return @"备注";}
        }else{
            IF_T_FF(row, column, 1, 0){return @"序号";}
            EF_T_FF(row, column, 2, 0){return @"名称";}
            EF_T_FF(row, column, 3, 0){return @"CAS号";}
            EF_T_FF(row, column, 4, 0){return @"限量";}
            EF_T_FF(row, column, 5, 0){return @"备注";}
        }
        
    }
    if (self.sourceDetailModel.kinds>=6){
        //
        IF_T_FF(row, column, 1, 1){return self.sourceDetailModel.uid;}
        EF_T_FF(row, column, 2, 1){return self.sourceDetailModel.englishName;}
        EF_T_FF(row, column, 3, 1){return self.sourceDetailModel.sourceCi;}
        EF_T_FF(row, column, 4, 1){return self.sourceDetailModel.sourceOdds;}
        EF_T_FF(row, column, 5, 1){return self.sourceDetailModel.sourceEye;}
        EF_T_FF(row, column, 6, 1){return self.sourceDetailModel.sourceSkin;}
        EF_T_FF(row, column, 7, 1){return self.sourceDetailModel.remark;}
        //
        IF_T_FF(row, column, 1, 2){return self.sourceDetailModel.chinaId;}
        EF_T_FF(row, column, 2, 2){return self.sourceDetailModel.chinaName;}
        EF_T_FF(row, column, 3, 2){return @"";}
        EF_T_FF(row, column, 4, 2){return @"";}
        EF_T_FF(row, column, 5, 2){return @"";}
        EF_T_FF(row, column, 6, 2){return @"";}
        EF_T_FF(row, column, 7, 2){return self.sourceDetailModel.remark;}
    }else{
        //
        IF_T_FF(row, column, 1, 1){return self.sourceDetailModel.uid;}
        EF_T_FF(row, column, 2, 1){return self.sourceDetailModel.englishName;}
        EF_T_FF(row, column, 3, 1){return self.sourceDetailModel.sourceCas;}
        EF_T_FF(row, column, 4, 1){return  self.sourceDetailModel.otherLimit;}
        EF_T_FF(row, column, 5, 1){return self.sourceDetailModel.remark;}
        //
        IF_T_FF(row, column, 1, 2){return self.sourceDetailModel.chinaId;}
        EF_T_FF(row, column, 2, 2){return self.sourceDetailModel.chinaName;}
        EF_T_FF(row, column, 3, 2){return @"";}
        EF_T_FF(row, column, 4, 2){return self.sourceDetailModel.chinaLimit;}
        EF_T_FF(row, column, 5, 2){return self.sourceDetailModel.remark;}
       
    }
    
   
    return @"";
}
-(UIColor*)listChart:(ListChartView *)list backgroundColorForRow:(NSInteger)row column:(NSInteger)column
{
    if (row == 0 )
    {
        return navColor;
    }
    return nil;
}
-(void)loadData:(NSString *)sid
{
    __weak __typeof(self)weakSelf = self;
    [[Api sharedApi] getSouceDetail:sid callback:^(ApiResponse * _Nonnull response, NSString * _Nonnull errorMsg) {
        if(response.code==0 && response.data !=nil)
        {
            weakSelf.sourceDetailModel = [SourceDetailModel modelWithDictionary:response.data];
            [weakSelf.chatView reloadData];
        }else{
            [BaseToast show:response.message];
        }
    }];
}

-(UILabel*)remarkLab
{
    if (!_remarkLab) {
        _remarkLab = [[UILabel alloc] initWithFrame:CGRectMake(10, SCREEN_HEIGHT-90,SCREEN_WIDTH-20, 40)];
        _remarkLab.font = [UIFont systemFontOfSize:15];
        _remarkLab.textColor = [UIColor redColor];
        _remarkLab.textAlignment = NSTextAlignmentLeft;;
        _remarkLab.numberOfLines=0;
        _remarkLab.text =@"注:不同产品类别中该物质的规定，请点击最下方的标签页查看";
        [self.view addSubview:_remarkLab];
    }
    return _remarkLab;
}
- (TYTabPagerBar *)tabBar {
    if (!_tabBar) {
        _tabBar = [[TYTabPagerBar alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-100, 44)];
        _tabBar.backgroundColor=navColor;
        _tabBar.layout.barStyle = TYPagerBarStyleProgressElasticView;
        _tabBar.autoScrollItemToCenter =NO;
        _tabBar.dataSource = self;
        _tabBar.delegate = self;
        [_tabBar registerClass:[TYTabPagerBarCell class] forCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier]];
        [self.view addSubview:_tabBar];
    }
    return _tabBar;
}
#pragma mark - TYTabPagerBarDataSource
- (NSInteger)numberOfItemsInPagerTabBar {
    return self.tabsArray.count;
}
- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    UICollectionViewCell<TYTabPagerBarCellProtocol> *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[TYTabPagerBarCell cellIdentifier] forIndex:index];
    cell.titleLabel.text = self.tabsArray[index];
    return cell;
}

#pragma mark - TYTabPagerBarDelegate
- (CGFloat)pagerTabBar:(TYTabPagerBar *)pagerTabBar widthForItemAtIndex:(NSInteger)index {
    NSString *title = self.tabsArray[index];
    return [pagerTabBar cellWidthForTitle:title];
}
- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    [_tabBar scrollToItemFromIndex:_currentIndex toIndex:index animate:YES];
    _currentIndex =index;
    if (index>self.sidsArray.count) {
        return;
    }
    TabbModel *tab = [self.sidsArray objectAtIndex:index];
    [self loadData:tab.sid];
}

@end
